﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MS.InformationManagement.Presenter.Support;
using System.Collections;
using MS.InformationManagement.Support;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Presenter.IBusquedaPrestamosDocumentalesView;
using System.Data;
using Microsoft.SharePoint;
using MS.InformationManagement.DAL.DTOs;
using System.Collections.Generic;
using System.Text;

namespace MS.InformationManagement.Controls._CONTROLTEMPLATES.InformationManagerControls
{
    public partial class BusquedaPrestamosDocumentales : BaseControl, IBusquedaPrestamosDocumentalesView
    {
        #region fields

        BusquedaPrestamosDocumentalesPresenter _presenter;

        #endregion

        #region Properties

        public string Observaciones { get; set; }
        public bool Result { get; set; }
        public Array UserList { get; set; }
        public List<RadicadoSearchDTO> RadicadosOwner { get; set; }
        public List<RadicadoSearchDTO> RadicadosResult { get; set; }
        public string Seccion { get; set; }
        public string SearchId { get; set; }
        public Guid GuidCurrentList
        {
            get { return new Guid(Page.Request.QueryString["lstID"]); }
        }
        public int PageIndex
        {
            get
            {
                return pgrTemplates.PageIndex;
            }
            set
            {
                pgrTemplates.PageIndex = value;
            }
        }
        public int PageSize
        {
            get
            {
                return pgrTemplates.PageSize;
            }
            set
            {
                pgrTemplates.PageSize = value;
            }
        }
        public int TotalRecords
        {
            get
            {
                return pgrTemplates.TotalRecords;
            }
            set
            {
                pgrTemplates.TotalRecords = value;
            }
        }

        #endregion

        #region Form Methods

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Page.RegisterAjaxLoaderPlugin();
            pgrTemplates.OnPageChanged += new Pager.PageChangeHandler(pgrTemplates_OnPageChanged);
            //pgrTemplates.NextLinkText = ">>";
            //pgrTemplates.PreviousLinkText = "<<";
            pgrTemplates.NumberOfPagesToDisplay = 10;
            PageIndex = 0;
            PageSize = 10;
            pgrTemplates.BuildPager();
            btnSearch.AddLoader();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            btnSearch.Click += new EventHandler(Action_Search);
            _presenter = new BusquedaPrestamosDocumentalesPresenter(this);
        }

        void Action_Search(object sender, EventArgs e)
        {
            PageIndex = 0;
            SearchId = txtBuscar.Text;
            _presenter.BuscarRadicado();
            pgrTemplates.BuildPager();
        }

        protected void gvRadicados_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            
        }

        protected void gvRadicados_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Controls[0].Visible = false;
                e.Row.Controls[1].Visible = false;
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton btn = (ImageButton)sender;
            GridViewRow row = (GridViewRow)btn.NamingContainer;
            DesplegarVentanaModal(SPContext.Current.Web.Url + "/_layouts/MS.InformationManagement.WP/SolicitudPrestamos.aspx?IdList=" + row.Cells[1].Text + "&ID=" + row.Cells[0].Text, "Sistema de Gestión Documental", 640, 460, this.Page);
        }

        void pgrTemplates_OnPageChanged(object sender, Controls.PageChangeEventArgs e)
        {
            SearchId = txtBuscar.Text;
            _presenter.BuscarRadicado();
            pgrTemplates.BuildPager();
        }

        public void DesplegarVentanaModal(string Url, string Titulo, Int32 ancho, Int32 alto, Page pagina)
        {
            StringBuilder scriptModal = new StringBuilder();
            scriptModal.Append("var showMyModalDialog = function() { var options = { ");
            scriptModal.Append("url : '");
            scriptModal.Append(Url);
            scriptModal.Append("', ");
            scriptModal.Append("title: '");
            scriptModal.Append(Titulo);
            scriptModal.Append("', width: ");
            scriptModal.Append(ancho.ToString());
            scriptModal.Append(", height: ");
            scriptModal.Append(alto.ToString());
            scriptModal.Append(", dialogReturnValueCallback: onDialogClose }; SP.UI.ModalDialog.commonModalDialogOpen(" + "'" + Url + "'" + ",options, onDialogClose, null); }; ");
            scriptModal.Append("function onDialogClose(dialogResult, returnValue) { ");
            scriptModal.Append("if (dialogResult == SP.UI.DialogResult.OK) { ");
            scriptModal.Append(" SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK); } ");
            scriptModal.Append(" } ");

            pagina.ClientScript.RegisterClientScriptBlock(pagina.GetType(), "Mensaje", scriptModal.ToString(), true);
            pagina.ClientScript.RegisterStartupScript(typeof(Page), "Precarga", "ExecuteOrDelayUntilScriptLoaded(showMyModalDialog, 'sp.ui.dialog.js');", true);
        }

        #endregion

        #region Implemented Methods

        public void Success()
        {
            gvRadicados.DataSource = RadicadosResult;
            gvRadicados.DataBind();
            if (RadicadosOwner != null)
            {
                if (RadicadosOwner.Count > 0)
                {
                    string OwnRadicados = "Los siguientes radicados se encuentran administrados por usted : ";
                    for (int i = 0; i < RadicadosOwner.Count; i++)
                    {
                        if (i == RadicadosOwner.Count - 1)
                            OwnRadicados += RadicadosOwner[i].IdRadicado;
                        else
                            OwnRadicados += RadicadosOwner[i].IdRadicado + " , ";
                    }
                    this._ShowAlertAndClose(OwnRadicados);
                }
            }                    
        }
        
        public new event Presenter.Support.Events.ErrorHandler OnError;

        public event Presenter.Support.Events.SuccessHandler OnSuccess;

        #endregion

       

        
        
        
    }
}
